package cn.czh.pet.service.impl;


import cn.czh.basic.exception.BusinessException;
import cn.czh.basic.service.IBaiduAiAuditService;
import cn.czh.basic.util.BaiduAiUtils;
import cn.czh.basic.util.LoginContext;
import cn.czh.org.domain.Employee;
import cn.czh.org.mapper.EmployeeMapper;
import cn.czh.pet.domain.Pet;
import cn.czh.pet.domain.PetDetail;
import cn.czh.pet.domain.PetOnlineAuditLog;
import cn.czh.pet.mapper.PetDetailMapper;
import cn.czh.pet.mapper.PetMapper;
import cn.czh.pet.mapper.PetOnlineAuditLogMapper;
import cn.czh.pet.service.IPetService;
import cn.czh.basic.service.impl.BaseServiceImpl;
import cn.czh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;



    @Override
    public void add(Pet pet) {
        super.add(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.save(petDetail);
        }
    }


    @Override
    public void update(Pet pet) {
        super.update(pet);
        if (pet.getPetDetail() != null) {
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPetId(pet.getId());
            //宠物详情更新：根据宠物详情id去更新
            petDetailMapper.update(petDetail);
        }
    }


    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }


    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //1.审核
            //审核文本
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            //审核图片
            Boolean boo2 =baiduAiAuditService.imageAudit(pet.getResources());
            if(boo1 && boo2){//审核成功
                //a.设置上架时间
                pet.setOnsaletime(new Date());
                //修改状态：将状态由0 - 1
                pet.setState(1);
                petMapper.update(pet);

                //b.添加上架审核日志 - 审核成功的日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);

            }else{//审核失败

                //添加上架审核日志 - 审核失败的日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setNote("审核失败");
                petOnlineAuditLogMapper.save(log);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        //修改状态：将状态由1 - 0
        petMapper.offsale(ids,new Date());
    }


}


